﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace _201206QuanLyBanHang.ChucNang
{
    public partial class DM_Chiphididuong : Form
    {
        private int machiphi;
        private string tenchiphi;
        private string ghichu;
        bool flat = false;
        private int sodong;
        private string sqlStr = "select machiphi,tenchiphi,ghichucpdd from chiphididuong order by machiphi,tenchiphi,ghichucpdd";
        private BLL_chiphididuong db = new BLL_chiphididuong();
        private DataSet ds = new DataSet();
        public DM_Chiphididuong()
        {
            InitializeComponent();
            DataBind(sqlStr);
        }
        public void DataBind(string sql)
        {
            ds = db.getchiphididuong(sql);
            dgchiphididuong.DataSource = ds.Tables[0];
            dgchiphididuong.Columns[0].HeaderText = "Mã số";
            dgchiphididuong.Columns[1].HeaderText = "Tên đầu tư";
            dgchiphididuong.Columns[2].HeaderText = "Ghi chú";
            dgchiphididuong.AllowUserToAddRows = false;

            //setdoituong();
            this.dgchiphididuong.AutoResizeColumns(
              DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            this.dgchiphididuong.ReadOnly = false;
            dgchiphididuong.AllowUserToAddRows = false;
            sodong = 0;
            sodong = ds.Tables[0].Rows.Count;
            this.grchiphididuong.Text = "Số lượng danh mục:  ";
            this.grchiphididuong.Text = this.grchiphididuong.Text + sodong;
        }
        private void luu()
        {
            //setdoituong(false, true);
            machiphi = int.Parse(this.txtma.Text);
            tenchiphi = this.txtten.Text;
            ghichu = this.txtghichu.Text;

            string err = "";
            if (!db.insertchiphididuong(ref err, machiphi, tenchiphi, ghichu))
            {
                MessageBox.Show(err.ToString());
            }
            DataBind(sqlStr);

        }
        private void DMSua()
        {
            string err = "Không thể sửa thông tin";
            machiphi = int.Parse(this.txtma.Text);
            tenchiphi = this.txtten.Text;
            ghichu = this.txtghichu.Text;

            if (!db.updateDautu(ref err, machiphi, tenchiphi, ghichu))
            {
                MessageBox.Show(err.ToString());
            }
            DataBind(sqlStr);
        }
        private void DMXoa()
        {
            try
            {
                machiphi = int.Parse(this.txtma.Text);
            }
            catch
            {
                MessageBox.Show("Chọn loại chi phí để xóa");
                return;
            }
            DialogResult result;
            result = MessageBox.Show("Bạn có muốn xóa không", "Xóa dữ liệu trong danh mục khu vực", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {
                string err = "Dữ liệu không thể xóa";
                if (!db.deletechiphididuong(ref err, machiphi))
                    MessageBox.Show(err);
                else
                    MessageBox.Show("Đã xóa thành công");
            }
            DataBind(sqlStr);
        }
        private void bin()
        {

            this.txtma.Text = dgchiphididuong[dgchiphididuong.Columns[0].Index, dgchiphididuong.CurrentRow.Index].Value.ToString();
            this.txtten.Text = dgchiphididuong[dgchiphididuong.Columns[1].Index, dgchiphididuong.CurrentRow.Index].Value.ToString();
            this.txtghichu.Text = dgchiphididuong[dgchiphididuong.Columns[2].Index, dgchiphididuong.CurrentRow.Index].Value.ToString();
            flat = true;
        }
        public int laystt()
        {
            int stt = 1;
            string sqlstring = "select max(machiphi) from chiphididuong";
            string err = "";
            object kq = db.layma(ref err, sqlstring);
            if (kq != System.DBNull.Value)
                stt = Convert.ToInt32(kq) + 1;
            return stt;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            flat = false;
            this.txtma.Text = laystt().ToString();
            this.txtten.Focus();
            this.txtten.Text = "";
            this.txtghichu.Text = "";

            //set lai trang thai nut
            this.Btnluu.Enabled = true;
            this.btnXoa.Enabled = true;
        }

        private void Btnluu_Click(object sender, EventArgs e)
        {
            if (flat)
                DMSua();
            else
                luu();
            //set doi tuong
            this.txtten.Focus();
            this.txtten.Text = "";
            this.txtghichu.Text = "";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            DMXoa();
            //set doi tuong
            this.txtten.Focus();
            this.txtten.Text = "";
            this.txtghichu.Text = "";
        }

        private void dgchiphididuong_Click(object sender, EventArgs e)
        {
            try
            {
                this.dgchiphididuong.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                this.dgchiphididuong.BeginEdit(false);
                this.dgchiphididuong.CurrentRow.Selected = true;
                this.bin();
                this.Btnluu.Enabled = true;
                this.btnXoa.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        public string ToTitleCase(string str)
        {
            str = str.ToLower();
            char[] charArr = str.ToCharArray();
            charArr[0] = char.ToUpper(charArr[0]);
            foreach (Match m in Regex.Matches(str, @"\s\S"))
            {
                charArr[m.Index + 1] = m.Value.ToUpper().Trim()[0];
            }
            return new string(charArr);
        }

        private void txtten_Leave(object sender, EventArgs e)
        {
            if (this.txtten.Text != "")
            {
                string t = txtten.Text;
                txtten.Text = ToTitleCase(t);
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            ChucNang.FormChiPhiKhac fr = new ChucNang.FormChiPhiKhac();
            fr.ktOn_Off(0);


            this.Close();
        }
    }
}
